In [28]:
%matplotlib inline
import matplotlib.pyplot as plt;
from jubiiworkflow.data import get_data
import pandas as pd
import numpy as np
from sklearn.decomposition import PCA
from sklearn.mixture import GaussianMixture
plt.style.use('seaborn');
In [66]:
data = get_data()
p = data.resample('W').sum().plot()
p.set_ylim(0, None);
In [67]:
pivoted = data.pivot_table('Total', index=data.index.time, columns=data.index.date)
pivoted.plot(legend=False, alpha=0.01);
In [17]:
X = pivoted.fillna(0).T.values
X.shape
Out[17]:
In [68]:
X2 = PCA(2, svd_solver='full').fit_transform(X)
X2.shape
Out[68]:
In [69]:
plt.scatter(X2[:, 0], X2[:, 1]);
In [59]:
gmm = GaussianMixture(2).fit(X)
labels = gmm.predict(X)
In [60]:
plt.scatter(X2[:, 0], X2[:, 1], c=labels, cmap='rainbow')
plt.colorbar();
In [61]:
fig, ax = plt.subplots(1, 2, figsize=(14, 6))
pivoted.T[labels == 0].T.plot(legend=False, alpha=0.1, ax=ax[0]);
pivoted.T[labels == 1].T.plot(legend=False, alpha=0.1, ax=ax[1]);
ax[0].set_title('Purple Cluster')
ax[1].set_title('Red Cluster');
In [26]:
dayofweek = pd.DatetimeIndex(pivoted.columns).dayofweek
plt.scatter(X2[:, 0], X2[:, 1], c=dayofweek, cmap='rainbow')
plt.colorbar();
In [27]:
dates = pd.DatetimeIndex(pivoted.columns)
dates[(labels == 0) & (dayofweek < 5)]
Out[27]: